<template>
	<view class="wrapper-pass">
		<view class="fixed-border"></view>
		<view class="contents">
			<view class="item flex">
				<label class="label" for="">输入旧密码</label>
				<view class="ipt"><input type="text" password v-model.trim="oldPassword" /></view>
			</view>
			<view class="item flex">
				<label class="label" for="">输入新密码</label>
				<view class="ipt"><input type="text" password v-model.trim="newPassword" /></view>
			</view>
			<view class="item flex">
				<label class="label" for="">确认密码</label>
				<view class="ipt"><input type="text" password v-model.trim="surePass" @blur="validate"/></view>
			</view>
		</view>
		<view class="btn">
			<button type="primary" @click="sub">提交</button>
		</view>
	</view>
</template>

<script>
	import Request from '@/request/request.js'
	export default {
		data() {
			return {
				oldPassword:'',//旧密码
				newPassword:'',//新密码
				surePass:'',//确认密码
			}
		},
		methods: {
			validate(){
				if(!this.oldPassword){
					uni.showToast({
					    title: '请输入旧密码!',
						icon:'none',
					    duration: 1500
					})
					return false
				}
				if(!this.newPassword){
					uni.showToast({
					    title: '请输入新密码!',
						icon:'none',
					    duration: 1500
					})
					return false
				}
				if(this.newPassword.length < 6){
					uni.showToast({
					    title: '新密码长度小于6!',
						icon:'none',
					    duration: 1500
					})
					return false
				}
				if(this.newPassword != this.surePass){
					uni.showToast({
					    title: '请重新确认新密码!',
						icon:'none',
					    duration: 1500
					})
					return false
				}
				return true;
			},
			sub(){
				if(this.validate()){
					// console.log(this.oldPassword)
					// console.log(this.newPassword)
					Request({
						url:'user/resetPwd',
						data:{"oldPassword":this.oldPassword,"newPassword":this.newPassword},
						}).then(res => {
							if(res.code == 500){
								uni.showToast({
								    title: res.msg,
									icon:'none',
								    duration: 1500
								})
							}else if(res.code == 0){
								uni.showToast({
								    title: '密码修改成功!',
									icon:'none',
								    duration: 1500
								})
								try {
								    uni.removeStorageSync('password');
								    uni.removeStorageSync('authorization');
									setTimeout(() => {
										uni.reLaunch({
											url:'/pages/login/login'
										})
									})
								} catch (e) {
								    // error
								}
							}
						}).catch(err => {})
				}
			}
		}
	}
</script>

<style lang="less" scoped>
  .wrapper-pass{
	  // margin-top: 1px;
	  height: 100%;
	  background-color: #FFFFFF;
	  padding-top: 1px;
	  .contents{
		  .item{
			  height: 110rpx;
			  border-bottom: 1px solid #F4F5F6;
			  padding: 0 20rpx;
			  align-items: center;
			  .label{
				  width: 180rpx;
				  text-align: right;
			  }
			  .ipt{
				  flex:1;
				  margin:0 60rpx 0 28rpx;
				  border: 1px solid #F4F5F6;
				  padding: 10rpx 20rpx;
				  input{
					  line-height: 20rpx;
				  }
			  }
		  }
	  }
	  .btn{
		  margin: 180rpx 30rpx 0;
	  }
  }
</style>
